﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Data;

namespace FreeProperty.Data
{
    public class NameValue : DataAccess
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _value;

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
    }
    public class DataAccess
    {
        [NonSerialized]
        protected SqlConnection connection;
        [NonSerialized]
        protected SqlCommand command;
        public static SqlDouble ToSqlDouble(Object ID)
        {
            SqlDouble siID;

            if (ID == DBNull.Value)
            {
                siID = SqlDouble.Null;
            }
            else
            {
                if (ID is SqlDouble)
                    return (SqlDouble)ID;
                siID = new SqlDouble((double)ID);
            }

            return siID;
        }

        public static SqlBoolean ToSqlBoolean(Object ID)
        {
            SqlBoolean siID;

            if (ID == DBNull.Value)
            {
                siID = SqlBoolean.Null;
            }
            else
            {
                if (ID is SqlBoolean)
                    return (SqlBoolean)ID;
                siID = new SqlBoolean((bool)ID);
            }

            return siID;
        }

        public static SqlInt32 ToSqlInt32(Object ID)
        {
            SqlInt32 siID;

            if (ID == DBNull.Value)
            {
                siID = SqlInt32.Null;
            }
            else
            {
                if (ID is SqlInt32)
                    return (SqlInt32)ID;
                siID = new SqlInt32(Convert.ToInt32(ID));
            }

            return siID;
        }

        public static SqlGuid ToSqlGuid(Object ID)
        {
            SqlGuid siID;

            if (ID == DBNull.Value)
            {
                siID = SqlGuid.Null;
            }
            else
            {
                if (ID is SqlGuid)
                    return (SqlGuid)ID;
                siID = new SqlGuid(new Guid(ID.ToString()));
            }

            return siID;
        }

        public static SqlDecimal ToSqlDecimal(Object ID)
        {
            SqlDecimal siID;

            if (ID == DBNull.Value)
            {
                siID = SqlDecimal.Null;
            }
            else
            {
                if (ID is SqlDecimal)
                    return (SqlDecimal)ID;
                siID = new SqlDecimal(Convert.ToDecimal(ID));
            }

            return siID;
        }

        public static SqlMoney ToSqlMoney(Object s)
        {
            SqlMoney siMoney;

            if (s == DBNull.Value)
            {
                siMoney = SqlMoney.Null;
            }
            else
            {
                if (s is SqlMoney)
                    return (SqlMoney)s;
                siMoney = new SqlMoney(Convert.ToDecimal(s));
            }

            return siMoney;
        }

        public static SqlDateTime ToSqlDateTime(Object s)
        {
            SqlDateTime sDateTime;

            if (s == DBNull.Value)
            {
                sDateTime = SqlDateTime.Null;
            }
            else
            {
                if (s is SqlDateTime)
                    return (SqlDateTime)s;
                sDateTime = new SqlDateTime(Convert.ToDateTime(s));
            }

            return sDateTime;
        }

        public static string ToString(Object s)
        {

            if (s == DBNull.Value)
            {
                return String.Empty;
            }
            else
            {
                return (string)s;
            }
        }
        public SqlCommand GetSPCommand(string storedProcedure)
        {
            connection = new SqlConnection(FreePropertySettings.ConnectionString);
            command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = storedProcedure;
            //command.CommandTimeout  = ;
            connection.Open();
            return command;
        }
        public void CloseConnection()
        {
            if (connection != null && connection.State == ConnectionState.Open)
            {
                connection.Close();
                connection.Dispose();
            }
        }
    }
}
